package com.wlcorp.forcazamis.web.action.pi;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.Region;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.wlcorp.forcazamis.business.pi.IPidetailService;
import com.wlcorp.forcazamis.business.stock.IStockService;
import com.wlcorp.forcazamis.domain.baseinfo.Customer;
import com.wlcorp.forcazamis.domain.baseinfo.Factory;
import com.wlcorp.forcazamis.domain.baseinfo.SampleSource;
import com.wlcorp.forcazamis.domain.pi.Pidetail;
import com.wlcorp.forcazamis.domain.pi.Pifitting;
import com.wlcorp.forcazamis.domain.pi.PiproductDetail;
import com.wlcorp.forcazamis.domain.sample.Sample;
import com.wlcorp.forcazamis.domain.sample.SubSample;
import com.wlcorp.forcazamis.domain.stock.Stock;
import com.wlcorp.forcazamis.domain.stock.StockFitting;
import com.wlcorp.forcazamis.domain.stock.StockProduct;
import com.wlcorp.forcazamis.domain.stock.StockProductDetail;
import com.wlcorp.forcazamis.domain.system.Company;
import com.wlcorp.forcazamis.domain.system.Department;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.FileFoldPath;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.CommonUtil;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.DateUtil;
import com.wlcorp.forcazamis.sys.util.FileUploadUtil;
import com.wlcorp.forcazamis.sys.util.Pagination;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.Validator;
import com.wlcorp.forcazamis.sys.util.WebUtil;
import com.wlcorp.forcazamis.sys.util.XLSFactory;

public class PidetailAction extends DispatchAction {
	public ActionForward pidetailManager(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<Department> departmentList = ContextUtil.getInstance().getDepartmentService()
				.getDepartmentList(null);
		request.setAttribute("departmentList", departmentList);
		return mapping.findForward("manager");
	}

	public ActionForward getPidetailList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Staff currentStaff = SessionValue.getInstance(request).getStaff();
		currentStaff = ContextUtil.getInstance().getStaffService().getStaffById(currentStaff.getStaffId());
		Pidetail pidetail = new Pidetail();
		WebUtil.setRequestToObject(request, pidetail);
		if (null != currentStaff.getDepartment()) {
			pidetail.setDepartmentId(currentStaff.getDepartment().getDepartmentId());
		}
		IPidetailService service = ContextUtil.getInstance().getPidetailService();
		int page = 1;
		int count = 0;
		if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
			page = Integer.parseInt(request.getParameter(Pagination.PAGE_ATTRIBUTE));
		}
		count = (int) service.getPidetailCount(pidetail).longValue();
		Pagination pagination = new Pagination();
		pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
		pagination.generatePageJSPFragment(request, count);
		pidetail.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<Pidetail> pidetailList = service.getPidetailList(pidetail);
		for (Iterator<Pidetail> it = pidetailList.iterator(); it.hasNext();) {
			while (it.hasNext()) {
				Pidetail pidetail1=(Pidetail)it.next();
				Long piCustomerId=pidetail1.getCustomer().getCustomerId();
				if ((null==currentStaff.getCustomers()||currentStaff.getCustomers().size()==0)&&!currentStaff.getRole().getRoleId().equals(new Long(1))&&!currentStaff.getRole().getRoleId().equals(new Long(11))) {
					it.remove();
				}
				if (false==currentStaff.getCustomers().isEmpty()&&!currentStaff.getCustomers().contains(pidetail1.getCustomer())&&!currentStaff.getRole().getRoleId().equals(new Long(1))&&!currentStaff.getRole().getRoleId().equals(new Long(11))) {
					for (Iterator<Customer> iterator = currentStaff.getCustomers().iterator(); iterator.hasNext();) {
						Customer staffCustomer = (Customer) iterator.next();
						if (staffCustomer.getCustomerId().equals(piCustomerId)||currentStaff.getRole().getRoleId().equals(new Long(1))||currentStaff.getRole().getRoleId().equals(new Long(11))) {
							break;
						}else{
							it.remove();
						}
					}
				}
//				for (Iterator<Customer> iterator = currentStaff.getCustomers().iterator(); iterator.hasNext();) {
//					Customer staffCustomer = (Customer) iterator.next();
//					if (staffCustomer.getCustomerId().equals(piCustomerId)||currentStaff.getRole().getRoleId().equals(new Long(1))||currentStaff.getRole().getRoleId().equals(new Long(11))) {
//						break;
//					}else{
//						it.remove();
//					}
//				}
			}
		}
		request.setAttribute("pidetailList", pidetailList);
		return mapping.findForward("list");
	}

	public ActionForward getPidetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Pidetail pidetail = new Pidetail();
		WebUtil.setRequestToObject(request, pidetail);
		pidetail = ContextUtil.getInstance().getPidetailService()
				.getPidetailById(pidetail.getPiid());
		if (null == pidetail) {
			pidetail = new Pidetail();
			pidetail.setPiDate(DateUtil.getCurrentDate());
			pidetail.setPriceItem("FOB SHANGHAI");
			pidetail.setTotalDiscount(1.0);
			pidetail.setCoinType(1);
			pidetail.setTransportTypeText("BY SEA");
		}
		List<SampleSource> sourceList = ContextUtil.getInstance().getSampleSourceService()
				.getSampleSourceList(null);
		request.setAttribute("sourceList", sourceList);
		request.setAttribute("pidetail", pidetail);
		return mapping.findForward("modify");
	}

	public ActionForward modifyPidetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Pidetail pidetail = new Pidetail();
		Staff currentStaff = SessionValue.getInstance(request).getStaff();
		currentStaff = ContextUtil.getInstance().getStaffService()
				.getStaffById(currentStaff.getStaffId());
		// 保存图片跟数据
		String filePath = null;
		SmartUpload smartUpload = FileUploadUtil.getSmartUpload(getServlet(),
				request, response);
		String piid = smartUpload.getRequest().getParameter("piid");
		if (!Validator.checkValueIsValid(piid)) {
			WebUtil.setRequestToObject(smartUpload.getRequest(), pidetail);
			if (null != currentStaff.getDepartment()) {
				pidetail.setDepartment(currentStaff.getDepartment());
			}
			pidetail.setCreator(currentStaff);
			pidetail.setCreatedDate(new Date(Calendar.getInstance()
					.getTimeInMillis()));
		} else {
			pidetail = ContextUtil.getInstance().getPidetailService()
					.getPidetailById(Long.valueOf(piid));
			WebUtil.setRequestToObject(smartUpload.getRequest(), pidetail);
			pidetail.setModifier(currentStaff);
			pidetail.setModifiedDate(new Date(Calendar.getInstance()
					.getTimeInMillis()));
		}
		try {
			long size = smartUpload.getFiles().getSize();
			if (size > 0) {
				String fileName = smartUpload.getFiles().getFile(0)
						.getFileName();
				fileName = WebUtil.gbToUnicode(fileName);
				filePath = FileFoldPath.PI_ATTACH_FOLD
						+ FileFoldPath.createFileName(fileName);
				smartUpload.getFiles().getFile(0).saveAs(
						getServlet().getServletContext().getRealPath(filePath));
				pidetail.setAttachPath(filePath);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}
		if (null == pidetail.getCoinType() || 0 == pidetail.getCoinType()) {
			pidetail.setCoinType(1);
		}
		WebUtil.setRequestToObject(request, pidetail);
		ContextUtil.getInstance().getPidetailService().savePidetail(pidetail);
		if (AppConstant.BASE_BEAN_STATUS_ERROR == pidetail.getStatus()) {
			WebUtil.setAlertMessage(request, pidetail.getMsg());
		} else {
			WebUtil.setAlertMessage(request, "操作成功！");
		}
		List<SampleSource> sourceList = ContextUtil.getInstance().getSampleSourceService()
				.getSampleSourceList(null);
		request.setAttribute("sourceList", sourceList);
		request.setAttribute("pidetail", pidetail);
		return mapping.findForward("modify");
	}

	public ActionForward deletePidetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Pidetail pidetail = new Pidetail();
		WebUtil.setRequestToObject(request, pidetail);
		IPidetailService service = ContextUtil.getInstance()
				.getPidetailService();
		pidetail = service.getPidetailById(pidetail.getPiid());
		if (null != pidetail) {
			service.deletePidetail(pidetail);
			if (AppConstant.BASE_BEAN_STATUS_ERROR == pidetail.getStatus()) {
				WebUtil.setAlertMessage(request, pidetail.getMsg());
			}
		}
		return getPidetailList(mapping, form, request, response);
	}
	
	public ActionForward deleteImage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		Pidetail pidetail=ContextUtil.getInstance().getPidetailService().getPidetailById(Long.parseLong(request.getParameter("piid")));
		pidetail.setAttachPath("");
		if (null!= pidetail) {
			ContextUtil.getInstance().getPidetailService().updatePidetail(pidetail);
			if (AppConstant.BASE_BEAN_STATUS_ERROR == pidetail.getStatus()) {
				WebUtil.setAlertMessage(request, pidetail.getMsg());
			} else {
				WebUtil.printText(response, "删除成功！");
			}
		}
		return getPidetail(mapping, form, request, response);
	}
	
	public ActionForward modifyPiproductDetail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		// 获取参数
		Long piid = Long.valueOf(request.getParameter("piid"));
		String[] productIdArray = request.getParameterValues("productId");
		String[] subSampleIdArray = request.getParameterValues("subSampleId");
		String[] custProdCodeArray = request.getParameterValues("custProdCode");
		String[] colorArray = request.getParameterValues("color");
		String[] prodCountArray = request.getParameterValues("prodCount");
		String[] priceArray = request.getParameterValues("price");
		String[] priceAddArray = request.getParameterValues("priceAdd");
		String[] totalSumArray = request.getParameterValues("totalSum");
		String[] typeArray = request.getParameterValues("type");
		String[] remarkArray = request.getParameterValues("remark");
		String[] orderNoArray = request.getParameterValues("orderNo");
		String[] pageNoArray = request.getParameterValues("pageNo");
		String[] isNewArray = request.getParameterValues("isNew");
		String[] oldCode = request.getParameterValues("oldCode");
		String[] ecolor = request.getParameterValues("ecolor");
		String[] ccolor = request.getParameterValues("ccolor");
		String[] size = request.getParameterValues("size");
		String[] ecolor1 = request.getParameterValues("ecolor1");
		String[] size1 = request.getParameterValues("size1");
		String[] col = request.getParameterValues("col");
		String[] col1 = request.getParameterValues("col1");

		List<PiproductDetail> piproductList = new ArrayList<PiproductDetail>();
		for (int i = 0; i < productIdArray.length; i++) {
			PiproductDetail piproductDetail = new PiproductDetail();
			SubSample sample = ContextUtil.getInstance().getSampleService()
					.getSubSampleById(Long.valueOf(subSampleIdArray[i]));
			piproductDetail.setSubSample(sample);
			piproductDetail.setCustProdCode(custProdCodeArray[i]);
			if (null != ccolor && (productIdArray.length == ccolor.length)) {
				piproductDetail.setColor(ccolor[i]);
				piproductDetail.setEcolor(ecolor1[i]);
				piproductDetail.setSize(size1[i]);
				piproductDetail.setOldCode(oldCode[i]);
				piproductDetail.setCol(col1[i]);
			} else {
				piproductDetail.setColor(colorArray[i]);
				piproductDetail.setEcolor(ecolor[i]);
				piproductDetail.setSize(size[i]);
				piproductDetail.setOldCode("");
				piproductDetail.setCol(col[i]);
			}
			piproductDetail.setProdCount(Long.valueOf(prodCountArray[i]));
			piproductDetail.setPrice(Double.valueOf(priceArray[i]));
			piproductDetail.setPriceAdd(Double.valueOf(priceAddArray[i]));
			piproductDetail.setTotalSum(Double.valueOf(totalSumArray[i]));
			piproductDetail.setType(typeArray[i]);
			piproductDetail.setRemark(remarkArray[i]);
			piproductDetail.setOrderNo(Integer.valueOf(orderNoArray[i]));
			piproductDetail.setPageNo(Integer.valueOf(pageNoArray[i]));
			piproductDetail.setIsNew(Integer.valueOf(isNewArray[i]));
			piproductDetail.setPiid(piid);
			piproductList.add(piproductDetail);
		}
		ContextUtil.getInstance().getPidetailService().savePiproductDetailList(
				piproductList, piid);
		WebUtil.setAlertMessage(request, "操作成功！");
		return getPiProductList(mapping, form, request, response);
	}

	public ActionForward deletePiproductDetail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		PiproductDetail piproductDetail = new PiproductDetail();
		WebUtil.setRequestToObject(request, piproductDetail);
		piproductDetail = ContextUtil.getInstance().getPidetailService()
				.getPiproductDetailById(piproductDetail.getProductId());
		ContextUtil.getInstance().getPidetailService().deletePiproductDetail(
				piproductDetail);
		WebUtil.printText(response, "删除成功！");
		return null;
	}

	public ActionForward getPifittingList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Long piid = Long.valueOf(request.getParameter("piid"));
		Pidetail pidetail = ContextUtil.getInstance().getPidetailService()
				.getPidetailById(piid);
		Pifitting fitting = new Pifitting();
		fitting.setPiid(piid);
		List<Pifitting> pifittingList = ContextUtil.getInstance().getPidetailService().getPifittingList(fitting);
		request.setAttribute("pidetail", pidetail);
		request.setAttribute("pifittingList", pifittingList);
		return mapping.findForward("pifittingList");
	}

	public ActionForward getPiProductList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Long piid = Long.valueOf(request.getParameter("piid"));
		Pidetail pidetail = ContextUtil.getInstance().getPidetailService()
				.getPidetailById(piid);
		PiproductDetail piproductDetail = new PiproductDetail();
		piproductDetail.setPiid(piid);
		List<PiproductDetail> piproductList = ContextUtil.getInstance().getPidetailService()
				.getPiproductDetailList(piproductDetail);
		request.setAttribute("pidetail", pidetail);
		request.setAttribute("piproductList", piproductList);
		return mapping.findForward("piproductList");
	}

	public ActionForward modifyPifitting(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		WebUtil.setResponseNoCache(response);
		Pifitting pifitting = new Pifitting();
		WebUtil.setRequestToObject(request, pifitting);
		ContextUtil.getInstance().getPidetailService().savePifitting(pifitting);
		WebUtil.printText(response, String.valueOf(pifitting.getFittingId()));
		return null;
	}

	public ActionForward deletePifitting(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		WebUtil.setResponseNoCache(response);
		Pifitting pifitting = new Pifitting();
		WebUtil.setRequestToObject(request, pifitting);
		pifitting = ContextUtil.getInstance().getPidetailService()
				.getPifittingById(pifitting.getFittingId());
		ContextUtil.getInstance().getPidetailService().deletePifitting(
				pifitting);
		WebUtil.printText(response, "1");
		return null;
	}

	public ActionForward selectPiProductTop(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("customerId", request.getParameter("customerId"));
		return mapping.findForward("selectPiproductTop");
	}

	public ActionForward selectPiProductList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		// Long piid = Long.valueOf(request.getParameter("piid"));
		Long customerId = Long.valueOf(request.getParameter("customerId"));
		String sampleCode = request.getParameter("sampleCode");
		String customerCode = request.getParameter("customerCode");
		// Pidetail pidetail =
		// ContextUtil.getInstance().getPidetailService().getPidetailById(piid);
		PiproductDetail piproductDetail = new PiproductDetail();
		piproductDetail.setCustomerId(customerId);

		Sample sample = new Sample();
		SubSample subSample = new SubSample();
		if (null != sampleCode) {
			sample.setSampleCode(sampleCode);
			subSample.setParentSample(sample);
			piproductDetail.setSubSample(subSample);
		} else {
			sample.setSampleCode("");
		}
		if (null != customerCode) {
			piproductDetail.setCustProdCode(customerCode);
		} else {
			piproductDetail.setCustProdCode("");
		}

		List<PiproductDetail> piproductList = ContextUtil.getInstance().getPidetailService()
				.getPiproductDetailListByCode(piproductDetail);
		// request.setAttribute("pidetail",pidetail);
		request.setAttribute("piproductList", piproductList);
		return mapping.findForward("selectPiproductList");
	}

	public ActionForward drawbackPI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		WebUtil.setResponseNoCache(response);
		Long piid = Long.valueOf(request.getParameter("piid"));
		Pidetail pidetail = ContextUtil.getInstance().getPidetailService()
				.getPidetailById(piid);

		ContextUtil.getInstance().getPidetailService().drawbackPI(pidetail);
		WebUtil.printText(response, "操作成功！");
		return null;
	}

	public ActionForward printPi(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Pidetail pidetail = new Pidetail();
		WebUtil.setRequestToObject(request, pidetail);
		PiproductDetail piproductDetail = new PiproductDetail();
		piproductDetail.setPiid(pidetail.getPiid());
		List<PiproductDetail> list = ContextUtil.getInstance().getPidetailService()
				.getPiproductDetailList(piproductDetail);
		pidetail = ContextUtil.getInstance().getPidetailService()
				.getPidetailById(pidetail.getPiid());
		Company company=ContextUtil.getInstance().getCompanyService().getCompany();
		request.setAttribute("piproductDetailList", list);
		request.setAttribute("pidetail", pidetail);
		request.setAttribute("company", company);
		return mapping.findForward("print");
	}

	public ActionForward exportPi(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Pidetail pidetail = new Pidetail();
		WebUtil.setRequestToObject(request, pidetail);
		PiproductDetail piproductDetail = new PiproductDetail();
		piproductDetail.setPiid(pidetail.getPiid());
		List<PiproductDetail> piproductDetailList = ContextUtil.getInstance().getPidetailService().getPiproductDetailList(piproductDetail);
		pidetail = ContextUtil.getInstance().getPidetailService().getPidetailById(pidetail.getPiid());
		Company company=ContextUtil.getInstance().getCompanyService().getCompany();

		//取出模板
		String excelPath = request.getSession().getServletContext().getRealPath("")+"/WEB-INF/PROFORMA_INVOICE_TEMPLATE.xls";
		XLSFactory xls=XLSFactory.getInstance();
		xls.setXLS(response, "PROFORMA_INVOICE_"+DateUtil.currentDate()+".xls");
		xls.getXLSTemplate1(excelPath);
		
		HSSFSheet sheet1=xls.getSheet();
		xls.getRow(0);
		xls.setCellExist(0, "FROM: "+CommonUtil.nullToStr(company.getEfullName()));
		HSSFRow row = sheet1.getRow((short) (0)); 
		row.getCell((short) (0)); 
		
		xls.getRow(1);
		xls.setCellExist(0, "ADD: "+CommonUtil.nullToStr(company.getEaddress()));
		xls.setCellExist(6, pidetail.getOutDate());
		
		xls.getRow(2);
		xls.setCellExist(0, "FAX: "+company.getFax());
		xls.setCellExist(2, CommonUtil.nullToStr(company.getElinkman()));
		xls.setCellExist(7, "P/I NO: "+pidetail.getPiCode());
		xls.setCellExist(10, "DATE: "+DateUtil.toDateString(new java.util.Date()));
		
		xls.getRow(3);
		xls.setCellExist(6, pidetail.getTransportTypeText());
		
		xls.getRow(4);
		xls.setCellExist(0, "TO: "+CommonUtil.nullToStr(pidetail.getCustomer().getCustomerFullName()));
		 
		xls.getRow(5);
		xls.setCellExist(0, "ADD: "+CommonUtil.nullToStr(pidetail.getCustomer().getAddress()));
		xls.setCellExist(6, CommonUtil.nullToStr(pidetail.getPriceItem()));
		xls.setCellExist(9, CommonUtil.nullToStr(pidetail.getTotalQuantity()));
		xls.setCellExist(10, CommonUtil.nullToStr(pidetail.getRePiQuantity()));
		xls.setCellExist(11, CommonUtil.nullToStr(pidetail.getNewPiQuantity()));
		 
		xls.getRow(6);
		xls.setCellExist(0, "ADD: "+CommonUtil.nullToStr(pidetail.getCustomer().getAddress()));
		xls.setCellExist(7, "Amount("+CommonUtil.nullToStr(pidetail.getCoinTypeName())+")");
		xls.setCellExist(9, CommonUtil.nullToStr(pidetail.getTotalSum()));
		xls.setCellExist(10, CommonUtil.nullToStr(pidetail.getRePiSum()));
		xls.setCellExist(11, CommonUtil.nullToStr(pidetail.getNewPiSum()));
		  
		xls.getRow(7);
		xls.setCellExist(0, "ATTN: "+CommonUtil.nullToStr(pidetail.getCustomer().getLinkmanName()));
		xls.setCellExist(6, CommonUtil.nullToStr(pidetail.getBalanceType()));
		 
		xls.getRow(9);
		xls.setCellExist(0, "ORDER: O/N "+CommonUtil.nullToStr(pidetail.getCustomerOgCode()));
		
		xls.getRow(10);
		xls.setCellExist(8, CommonUtil.nullToStr(pidetail.getCoinTypeName())+"/PC");

		int listSize=piproductDetailList.size();
		for (int i = 0; i < listSize; i++) {
			PiproductDetail piproduct=(PiproductDetail) piproductDetailList.get(i);
			
			xls.createRow(11+i);
			xls.setCellCreate(0, String.valueOf(i+1));
			xls.setCellCreate(1, CommonUtil.nullToStr(piproduct.getCustProdCode()));
			xls.setCellCreate(2, CommonUtil.nullToStr(piproduct.getSubSample().getParentSample().getSampleCode()));
			xls.setCellCreate(3, CommonUtil.nullToStr(piproduct.getOldCode()));
			xls.setCellCreate(4, CommonUtil.nullToStr(piproduct.getSize()));
			xls.setCellCreate(5, CommonUtil.nullToStr(piproduct.getCol()));
			
			//COL.DESC
			if(!CommonUtil.nullToStr(piproduct.getCol()).equals("")&&!CommonUtil.nullToStr(piproduct.getEcolor()).equals("")){
				xls.setCellCreate(6, piproduct.getCol()+"+"+piproduct.getEcolor());
			}
			if(CommonUtil.nullToStr(piproduct.getCol()).equals("")&&!CommonUtil.nullToStr(piproduct.getEcolor()).equals("")){
				xls.setCellCreate(6, piproduct.getEcolor());
			}
			if(CommonUtil.nullToStr(piproduct.getEcolor()).equals("")&&!CommonUtil.nullToStr(piproduct.getCol()).equals("")){
				xls.setCellCreate(6, piproduct.getCol());
			}
 
			xls.setCellCreate(7, piproduct.getProdCount());
			//USD/PC
			if(piproduct.getPrice()!=0.0&&piproduct.getPriceAdd()!=0.0){
				xls.setCellCreate(8, piproduct.getPrice()+piproduct.getPriceAdd());
			}
			if(piproduct.getPrice()!=0.0&&piproduct.getPriceAdd()==0.0){
				xls.setCellCreate(8, piproduct.getPrice());
			}
			if(piproduct.getPrice()==0.0&&piproduct.getPriceAdd()!=0.0){
				xls.setCellCreate(8, piproduct.getPriceAdd());
			}
			 
			xls.setCellCreate(9, piproduct.getTotalSum().toString());
			xls.setCellCreate(10, CommonUtil.nullToStr(piproduct.getRemark()));
		}
		
		if(null!=pidetail.getCard()){
			xls.getSheet().addMergedRegion(new Region(10+listSize+2, (short) 0, 10+listSize+2, (short) 11));
			if(pidetail.getCard().equals(AppConstant.CARD_USD)){
				xls.createRow(10+listSize+2);
				xls.setCellCreate(0, AppConstant.CARD_USD_INFO_EXPORT);
				xls.setCellStyleBold(xls.getCell());
				xls.setWrapText(xls.getCell());
			}
			if(pidetail.getCard().equals(AppConstant.CARD_EUR)){
				xls.createRow(10+listSize+2);
				xls.setCellCreate(0, AppConstant.CARD_EUR_INFO_EXPORT);
				xls.setCellStyleBold(xls.getCell());
				xls.setWrapText(xls.getCell());
			}
			if(pidetail.getCard().equals(AppConstant.CARD_RMB)){
				
			}
		}
		
		xls.createRow(10+listSize+4);
		xls.setCellCreate(0, pidetail.getMemo());
		xls.setCellStyleBold(xls.getCell());
		
//		String memo=pidetail.getMemo();
//		if(null!=memo&&!memo.equals("")){
//			//10代表前面应除外的行数,2代数空格两行
//			xls.createRow(10+listSize+2);
//			xls.setCellCreate(0, "BANK INFORMATION:");
//			xls.setCellStyleBold(xls.getCell());
//			
//			xls.createRow(10+listSize+3);
//			xls.setCellCreate(0, memo.substring(memo.indexOf("BENEFICIARY"), memo.indexOf("BENEFICIARY'S BANK")));
//			xls.setCellStyleBold(xls.getCell());
//			
//			xls.createRow(10+listSize+4);
//			xls.setCellCreate(0, memo.substring(memo.indexOf("BENEFICIARY'S BANK"), memo.indexOf("BENEFICIARY'S ACCOUNT")));
//			xls.setCellStyleBold(xls.getCell()); 
//			
//			xls.createRow(10+listSize+5);
//			xls.setCellCreate(0, memo.substring(memo.indexOf("BENEFICIARY'S ACCOUNT"), memo.indexOf("SWIFT")));
//			xls.setCellStyleBold(xls.getCell());
//			
//			xls.createRow(10+listSize+6);
//			xls.setCellCreate(0, memo.substring(memo.indexOf("SWIFT")));
//			xls.setCellStyleBold(xls.getCell());
//		}
		
		xls.exportXLS(response);
		return null;
	}
	
	public ActionForward stockBuild(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Long piid=Long.parseLong(request.getParameter("piid"));
		Pidetail pidetail = ContextUtil.getInstance().getPidetailService().getPidetailById(piid);
		List<PiproductDetail> piproductList=ContextUtil.getInstance().getPidetailService().getPiproductDetailListByPidetailId(piid);
		List<Pifitting> pifittingList=ContextUtil.getInstance().getPidetailService().getPifittingListByPidetailId(piid);
		List<Factory> factoryList=ContextUtil.getInstance().getFactoryService().getFactoryList(null);
		
		request.setAttribute("pidetail",pidetail);
		request.setAttribute("piproductList",piproductList);
		request.setAttribute("pifittingList",pifittingList);
		request.setAttribute("factoryList",factoryList);
		return mapping.findForward("build");
	}

	public ActionForward stockProductBuild(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff currentStaff = SessionValue.getInstance(request).getStaff();
		Long piid = Long.valueOf(request.getParameter("piid"));
		IPidetailService pidetailService=ContextUtil.getInstance().getPidetailService();
		IStockService stockService=ContextUtil.getInstance().getStockService();
		Pidetail pidetail = pidetailService.getPidetailById(piid);
		String stockCode=pidetail.getPiCode().substring(1,3)+pidetail.getPiCode().substring(4);
		System.out.println(stockCode);
		String[] selectFlag = request.getParameterValues("selectFlag");
		String[] factory = request.getParameterValues("factory");
		
		if(null==selectFlag){
			WebUtil.setAlertMessage(request, "至少要选择一条产品！");
			return stockBuild(mapping,form,request,response);
		}
		if(null==factory){
			WebUtil.setAlertMessage(request, "请选择工厂！");
			return stockBuild(mapping,form,request,response);
		}
		
		List<String> list=Arrays.asList(factory);
		List<String> factoryIdList = new ArrayList<String>(list);
		
		Iterator<String> it = factoryIdList.iterator(); 
		while(it.hasNext()){ 
			if(((String)it.next()).equals("0")){
				it.remove();
			}
			
		} 
 
		if (factoryIdList.size()==selectFlag.length) {
			for (int i = 0; i < selectFlag.length; i++) {
				Long productId=Long.parseLong(selectFlag[i]);
				PiproductDetail piproductDetail=pidetailService.getPiproductDetailById(productId);
				Long factoryId=Long.parseLong((String)factoryIdList.get(i));
				
				Stock stockExist=stockService.getStockByStockCodeAndFactoryId(stockCode, factoryId);
				//查看是否已有此PI生成过的采购合同
				if (null==stockExist) {
					//采购合同插入
					Stock stock=new Stock();
					stock.setCustomerId(pidetail.getCustomer().getCustomerId());
					stock.setStockCode(stockCode);
					stock.setStockLevel(0);
					stock.setStockType("A");//默认为一般紧急
					stock.setStockDate(pidetail.getPiDate());
					//Factory
					Factory f=new Factory();
					f.setFactoryId(factoryId);
					stock.setFactory(f);
					stock.setDeliveryDate(pidetail.getOutDate());
					stock.setCreator(currentStaff);
					stock.setCreatedDate(new Date(Calendar.getInstance().getTimeInMillis())); 
					stock.setState(AppConstant.BILL_STATE_SUBMITED);//合同状态 1:编辑中
//					if(null!=pidetail.getTotalSum()&&!pidetail.getTotalSum().equals("")){
//						stock.setTotalPrice(pidetail.getTotalSum());//?合同总金额;
//					}
//					
//					if(null!=pidetail.getTotalQuantity()&&!pidetail.getTotalQuantity().equals("")){
//						stock.setTotalGlasses(Integer.parseInt(pidetail.getTotalQuantity().toString()));//?镜架总付数;
//					}
					
					stock.setDeleteFlag(false);
					ContextUtil.getInstance().getStockService().saveStock(stock);
					//把刚保存记录的ID取出来
					Long maxStockId=stockService.getMaxStockId();
					
					List<StockProduct> stockProductList=stockService.getStockProductListByStockId(maxStockId);
					if (null==stockProductList||stockProductList.size()==0) {
							//采购合同产品插入
							StockProduct stockProduct=new StockProduct();
							Stock stockTemp=new Stock();
							stockTemp.setStockId(maxStockId);
							stockProduct.setStock(stock);
							stockProduct.setDeleteFlag(false);
							stockProduct.setMyCode(piproductDetail.getSubSample().getParentSample().getSampleCode());// 我司型号
//							stockProduct.setFactoryCode(piproductDetail.getSubSample().getParentSample().getFactoryCode());
							stockProduct.setCustomerOrderCode(piproductDetail.getCustProdCode());
							stockProduct.setFrameType(0);// 镜架类型默认为0:金属架
							stockProduct.setFigureSize(piproductDetail.getSubSample().getSize());
							stockProduct.setOrderSize(piproductDetail.getSize());//订单尺寸
							stockProduct.setProductImage(piproductDetail.getSubSample().getParentSample().getImagePath());// 产品附图
							stockService.saveStockProduct(stockProduct);
						
							//查出刚保存的记录
							Long stockProductId=stockService.getMaxStockProductId();
							StockProduct stockProductTmp=new StockProduct();
							stockProductTmp.setProductId(stockProductId);
						
							StockProductDetail stockProductDetail=new StockProductDetail();
							stockProductDetail.setStockProduct(stockProductTmp);
							stockProductDetail.setLetteringCode(piproductDetail.getCol());
							stockProductDetail.setLetteringSize(piproductDetail.getSize());
							stockProductDetail.setPiColor(piproductDetail.getEcolor());
							stockProductDetail.setLetteringColor(piproductDetail.getColor());
							stockProductDetail.setAmount(piproductDetail.getProdCount());
							stockProductDetail.setUnitPrice(Float.valueOf(piproductDetail.getPrice().toString()));
							stockProductDetail.setLetteringCode(piproductDetail.getCustProdCode());
							stockService.saveStockProductDetail(stockProductDetail);
						
							Stock stocktmp=stockService.getStockById(maxStockId);
							stocktmp.setTotalGlasses(Integer.parseInt(piproductDetail.getProdCount().toString()));
							Double prices=piproductDetail.getPrice()*piproductDetail.getProdCount();
							stocktmp.setTotalPrice(Float.valueOf(prices.toString()));
							stockService.updateStock(stocktmp);
						}else{
							for (int j = 0; j < stockProductList.size(); j++) {
								StockProduct stockProductExist=stockProductList.get(j);
								// 我司型号与订单尺寸比较
								if (stockProductExist.getMyCode().equals(piproductDetail.getSubSample().getParentSample().getSampleCode())&&stockProductExist.getOrderSize().equals(piproductDetail.getSize())) {
									StockProductDetail stockProductDetail=new StockProductDetail();
									stockProductDetail.setStockProduct(stockProductExist);
									stockProductDetail.setLetteringCode(piproductDetail.getCol());
									stockProductDetail.setLetteringSize(piproductDetail.getSize());
									stockProductDetail.setPiColor(piproductDetail.getEcolor());
									stockProductDetail.setLetteringColor(piproductDetail.getColor());
									stockProductDetail.setAmount(piproductDetail.getProdCount());
									stockProductDetail.setUnitPrice(Float.valueOf(piproductDetail.getPrice().toString()));
									stockProductDetail.setLetteringCode(piproductDetail.getCustProdCode());
									stockService.saveStockProductDetail(stockProductDetail);
									Stock stocktmp=stockService.getStockById(maxStockId);
									int totoalGlasses=stocktmp.getTotalGlasses();
									float totoalPrice=stocktmp.getTotalPrice();
									stocktmp.setTotalGlasses(totoalGlasses+Integer.parseInt(piproductDetail.getProdCount().toString()));
									Double prices=piproductDetail.getPrice()*piproductDetail.getProdCount();
									stocktmp.setTotalPrice(totoalPrice+Float.valueOf(prices.toString()));
									stockService.updateStock(stocktmp);
								} 
							}
							
							StockProduct stockProductLastest=stockProductList.get(stockProductList.size()-1);
							if (!stockProductLastest.getMyCode().equals(piproductDetail.getSubSample().getParentSample().getSampleCode())||!stockProductLastest.getOrderSize().equals(piproductDetail.getSize())) {
								//采购合同产品插入
								StockProduct stockProduct=new StockProduct();
								stockProduct.setStock(stockExist);
								stockProduct.setDeleteFlag(false);
								stockProduct.setMyCode(piproductDetail.getSubSample().getParentSample().getSampleCode());// 我司型号
//								stockProduct.setFactoryCode(piproductDetail.getSubSample().getParentSample().getFactoryCode());
								stockProduct.setCustomerOrderCode(piproductDetail.getCustProdCode());
								stockProduct.setFrameType(0);// 镜架类型默认为0:金属架
								stockProduct.setFigureSize(piproductDetail.getSubSample().getSize());
								stockProduct.setOrderSize(piproductDetail.getSize());
								stockProduct.setProductImage(piproductDetail.getSubSample().getParentSample().getImagePath());// 产品附图
								stockService.saveStockProduct(stockProduct);
								
								//查出刚保存的记录
								Long stockProductId=stockService.getMaxStockProductId();
								StockProduct stockProductTmp=new StockProduct();
								stockProductTmp.setProductId(stockProductId);
								
								StockProductDetail stockProductDetail=new StockProductDetail();
								stockProductDetail.setStockProduct(stockProductTmp);
								stockProductDetail.setLetteringCode(piproductDetail.getCol());
								stockProductDetail.setLetteringSize(piproductDetail.getSize());
								stockProductDetail.setPiColor(piproductDetail.getEcolor());
								stockProductDetail.setLetteringColor(piproductDetail.getColor());
								stockProductDetail.setAmount(piproductDetail.getProdCount());
								stockProductDetail.setUnitPrice(Float.valueOf(piproductDetail.getPrice().toString()));
								stockProductDetail.setLetteringCode(piproductDetail.getCustProdCode());
								stockService.saveStockProductDetail(stockProductDetail);
								
								int totoalGlasses=stockExist.getTotalGlasses();
								float totoalPrice=stockExist.getTotalPrice();
								stockExist.setTotalGlasses(totoalGlasses+Integer.parseInt(piproductDetail.getProdCount().toString()));
								Double prices=piproductDetail.getPrice()*piproductDetail.getProdCount();
								stockExist.setTotalPrice(totoalPrice+Float.valueOf(prices.toString()));
								stockService.updateStock(stockExist);
							}
						}
				}else{
					List<StockProduct> stockProductList=stockService.getStockProductListByStockId(stockExist.getStockId());
					if (null==stockProductList||stockProductList.size()==0) {
						//采购合同产品插入
						StockProduct stockProduct=new StockProduct();
						stockProduct.setStock(stockExist);
						stockProduct.setDeleteFlag(false);
						stockProduct.setMyCode(piproductDetail.getSubSample().getParentSample().getSampleCode());// 我司型号
//						stockProduct.setFactoryCode(piproductDetail.getSubSample().getParentSample().getFactoryCode());
						stockProduct.setCustomerOrderCode(piproductDetail.getCustProdCode());
						stockProduct.setFrameType(0);// 镜架类型默认为0:金属架
						stockProduct.setFigureSize(piproductDetail.getSubSample().getSize());
						stockProduct.setOrderSize(piproductDetail.getSize());//订单尺寸
						stockProduct.setProductImage(piproductDetail.getSubSample().getParentSample().getImagePath());// 产品附图
						stockService.saveStockProduct(stockProduct);
						
						//查出刚保存的记录
						Long stockProductId=stockService.getMaxStockProductId();
						StockProduct stockProductTmp=new StockProduct();
						stockProductTmp.setProductId(stockProductId);
						
						StockProductDetail stockProductDetail=new StockProductDetail();
						stockProductDetail.setStockProduct(stockProductTmp);
						stockProductDetail.setLetteringCode(piproductDetail.getCol());
						stockProductDetail.setLetteringSize(piproductDetail.getSize());
						stockProductDetail.setPiColor(piproductDetail.getEcolor());
						stockProductDetail.setLetteringColor(piproductDetail.getColor());
						stockProductDetail.setAmount(piproductDetail.getProdCount());
						stockProductDetail.setUnitPrice(Float.valueOf(piproductDetail.getPrice().toString()));
						stockProductDetail.setLetteringCode(piproductDetail.getCustProdCode());
						stockService.saveStockProductDetail(stockProductDetail);
						
						int totoalGlasses=stockExist.getTotalGlasses();
						float totoalPrice=stockExist.getTotalPrice();
						stockExist.setTotalGlasses(totoalGlasses+Integer.parseInt(piproductDetail.getProdCount().toString()));
						Double prices=piproductDetail.getPrice()*piproductDetail.getProdCount();
						stockExist.setTotalPrice(totoalPrice+Float.valueOf(prices.toString()));
						stockService.updateStock(stockExist);
					}else{
						StockProduct stockProductLastest=stockProductList.get(stockProductList.size()-1);
						for (int j = 0; j < stockProductList.size(); j++) {
							StockProduct stockProductExist=stockProductList.get(j);
							// 我司型号与订单尺寸比较
							if (stockProductExist.getMyCode().equals(piproductDetail.getSubSample().getParentSample().getSampleCode())&&stockProductExist.getOrderSize().equals(piproductDetail.getSize())) {
								StockProductDetail stockProductDetail=new StockProductDetail();
								stockProductDetail.setStockProduct(stockProductExist);
								stockProductDetail.setLetteringCode(piproductDetail.getCol());
								stockProductDetail.setLetteringSize(piproductDetail.getSize());
								stockProductDetail.setPiColor(piproductDetail.getEcolor());
								stockProductDetail.setLetteringColor(piproductDetail.getColor());
								stockProductDetail.setAmount(piproductDetail.getProdCount());
								stockProductDetail.setUnitPrice(Float.valueOf(piproductDetail.getPrice().toString()));
								stockProductDetail.setLetteringCode(piproductDetail.getCustProdCode());
								stockService.saveStockProductDetail(stockProductDetail);
								int totoalGlasses=stockExist.getTotalGlasses();
								float totoalPrice=stockExist.getTotalPrice();
								stockExist.setTotalGlasses(totoalGlasses+Integer.parseInt(piproductDetail.getProdCount().toString()));
								Double prices=piproductDetail.getPrice()*piproductDetail.getProdCount();
								stockExist.setTotalPrice(totoalPrice+Float.valueOf(prices.toString()));
								stockService.updateStock(stockExist);
							}
						}
						if (!stockProductLastest.getMyCode().equals(piproductDetail.getSubSample().getParentSample().getSampleCode())||!stockProductLastest.getOrderSize().equals(piproductDetail.getSize())) {
							//采购合同产品插入
							StockProduct stockProduct=new StockProduct();
							stockProduct.setStock(stockExist);
							stockProduct.setDeleteFlag(false);
							stockProduct.setMyCode(piproductDetail.getSubSample().getParentSample().getSampleCode());// 我司型号
//							stockProduct.setFactoryCode(piproductDetail.getSubSample().getParentSample().getFactoryCode());
							stockProduct.setCustomerOrderCode(piproductDetail.getCustProdCode());
							stockProduct.setFrameType(0);// 镜架类型默认为0:金属架
							stockProduct.setFigureSize(piproductDetail.getSubSample().getSize());
							stockProduct.setOrderSize(piproductDetail.getSize());
							stockProduct.setProductImage(piproductDetail.getSubSample().getParentSample().getImagePath());// 产品附图
							stockService.saveStockProduct(stockProduct);
							
							//查出刚保存的记录
							Long stockProductId=stockService.getMaxStockProductId();
							StockProduct stockProductTmp=new StockProduct();
							stockProductTmp.setProductId(stockProductId);
							
							StockProductDetail stockProductDetail=new StockProductDetail();
							stockProductDetail.setStockProduct(stockProductTmp);
							stockProductDetail.setLetteringCode(piproductDetail.getCol());
							stockProductDetail.setLetteringSize(piproductDetail.getSize());
							stockProductDetail.setPiColor(piproductDetail.getEcolor());
							stockProductDetail.setLetteringColor(piproductDetail.getColor());
							stockProductDetail.setAmount(piproductDetail.getProdCount());
							stockProductDetail.setUnitPrice(Float.valueOf(piproductDetail.getPrice().toString()));
							stockProductDetail.setLetteringCode(piproductDetail.getCustProdCode());
							stockService.saveStockProductDetail(stockProductDetail);
							
							int totoalGlasses=stockExist.getTotalGlasses();
							float totoalPrice=stockExist.getTotalPrice();
							stockExist.setTotalGlasses(totoalGlasses+Integer.parseInt(piproductDetail.getProdCount().toString()));
							Double prices=piproductDetail.getPrice()*piproductDetail.getProdCount();
							stockExist.setTotalPrice(totoalPrice+Float.valueOf(prices.toString()));
							stockService.updateStock(stockExist);
						}
					}
				}
			}
		}else{
			WebUtil.setAlertMessage(request, "选择的记录不一致！");
			return stockBuild(mapping,form,request,response);
		}
		WebUtil.setAlertMessage(request, "操作成功！");
		return stockBuild(mapping,form,request,response);
	}
	
	public ActionForward stockFittingBuild(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff currentStaff = SessionValue.getInstance(request).getStaff();
		
		Long piid = Long.valueOf(request.getParameter("piid"));
		Pidetail pidetail = ContextUtil.getInstance().getPidetailService().getPidetailById(piid);
		String stockCode=pidetail.getPiCode().substring(1,2)+pidetail.getPiCode().substring(3);
		String[] selectFlag = request.getParameterValues("selectFlag2");
		String[] factory = request.getParameterValues("factory2");
		if(null==selectFlag){
			WebUtil.setAlertMessage(request, "至少要选择一条产品！");
			return stockBuild(mapping,form,request,response);
		}
		if(null==factory){
			WebUtil.setAlertMessage(request, "请选择工厂！");
			return stockBuild(mapping,form,request,response);
		}
		List<String> list=Arrays.asList(factory);
		List<String> factoryIdList = new ArrayList<String>(list);
		for (int i = 0; i < factoryIdList.size(); i++) {
			String id=(String)factoryIdList.get(i);
			System.out.println("id="+id+"#");
			if(id.equals("")){
				factoryIdList.remove(i);
			}
		}
		if (factoryIdList.size()==selectFlag.length) {
			for (int i = 0; i < selectFlag.length; i++) {
				Long fittingId=Long.parseLong(selectFlag[i]);
				Pifitting pifitting=ContextUtil.getInstance().getPidetailService().getPifittingById(fittingId);
				Long factoryId=Long.parseLong((String)factoryIdList.get(i));
				
				Stock stockExist=ContextUtil.getInstance().getStockService().getStockByStockCodeAndFactoryId(stockCode, factoryId);
				//查看是否已有此PI生成过的采购合同
				if (null==stockExist) {
					//采购合同插入
					Stock stock=new Stock();
					stock.setStockCode(stockCode);
					stock.setStockLevel(0);
					stock.setStockType("A");//默认为一般紧急
					stock.setStockDate(pidetail.getPiDate());
					//Factory
					Factory f=new Factory();
					f.setFactoryId(factoryId);
					stock.setFactory(f);
					stock.setDeliveryDate(pidetail.getOutDate());
					stock.setCreator(currentStaff);
					stock.setCreatedDate(new Date(Calendar.getInstance().getTimeInMillis())); 
					stock.setState(AppConstant.BILL_STATE_SUBMITED);//合同状态 1:编辑中
					stock.setTotalPrice(new Float(0.00));//?合同总金额;
					stock.setTotalGlasses(0);//?镜架总付数;
					stock.setDeleteFlag(false);
					ContextUtil.getInstance().getStockService().saveStock(stock);
					//把刚保存记录的ID取出来
					Long maxStockId=ContextUtil.getInstance().getStockService().getMaxStockId();
					
					//采购合同配件插入
					StockFitting stockFitting=new StockFitting();
					Stock stockTemp=new Stock();
					stockTemp.setStockId(maxStockId);
					stockFitting.setEname(pifitting.getEname());
					stockFitting.setCname("");
					stockFitting.setEspec(pifitting.getEspec());
					stockFitting.setCspec("");
					stockFitting.setAmount(pifitting.getFittingCount());
					stockFitting.setPrice(pifitting.getUnitPrice());
					stockFitting.setSum(pifitting.getTotalSum());
					stockFitting.setMemo("");
					stockFitting.setDeleteFlag(false);
					ContextUtil.getInstance().getStockService().saveStockFitting(stockFitting);
				}else{
					//采购合同配件插入
					StockFitting stockFitting=new StockFitting();
					stockFitting.setStock(stockExist);
					stockFitting.setEname(pifitting.getEname());
					stockFitting.setCname("");
					stockFitting.setEspec(pifitting.getEspec());
					stockFitting.setCspec("");
					stockFitting.setAmount(pifitting.getFittingCount());
					stockFitting.setPrice(pifitting.getUnitPrice());
					stockFitting.setSum(pifitting.getTotalSum());
					stockFitting.setMemo("");
					stockFitting.setDeleteFlag(false);
					ContextUtil.getInstance().getStockService().saveStockFitting(stockFitting);
				}
			}
		}else{
			WebUtil.setAlertMessage(request, "选择的记录不一致！");
			return stockBuild(mapping,form,request,response);
		}
		WebUtil.setAlertMessage(request, "操作成功！");
		return stockBuild(mapping,form,request,response);
	}
}
